//
//  LGTeamManageController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/2/16.
//

#import "LGTeamManageController.h"
//团队详情
#import "LGTeamDetailController.h"
//创建团队
#import "LGCreateTeamController.h"
//加入团队
#import "LGJoinTeamController.h"
//申请记录
#import "LGTeamRecordController.h"

#import "LGTeamManageCell.h"
#import "SDImageCache.h"
#define   buttonTop  (Screen_H-topBarHeight-viewPix(85))

@interface LGTeamManageController ()<UITableViewDelegate,UITableViewDataSource>
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)UIButton *recordBtn;
/**   */
@property (nonatomic , strong)UIButton *joinBtn;
/**   */
@property (nonatomic , strong)UIButton *createBtn;
/**   */
@property (nonatomic , strong)NSArray *dataArray;

@end

@implementation LGTeamManageController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"团队管理";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.recordBtn];
    [self.view addSubview:self.tableView];
    [self emptyViewWithSuperView:self.view frame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight-viewPix(90))];
    [self.view addSubview:self.joinBtn];
    [self.view addSubview:self.createBtn];
    [[SDImageCache sharedImageCache] clearWithCacheType:SDImageCacheTypeAll completion:^{
        [self.tableView reloadData];
    }];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/my.teams/teamsForUser" parameters:@{} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        [self.tableView.mj_header endRefreshing];
        if(code == 200){
            self.roleType = LGNSString(returnData[@"role_type"]);
            self.dataArray = returnData[@"list"];
        }
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:LGEmptyPicName content:@"暂无团队" offset:0];
        }
        [self.tableView reloadData];
    } error:^(NSError *error) {
        self.isfirst = NO;
        [self.tableView.mj_header endRefreshing];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:LGEmptyPicName content:LGErrorMessage offset:0];
        }
    }];
}

-(void)setDataArray:(NSArray *)dataArray{
    _dataArray = dataArray;
    BOOL created = NO;
    for (NSDictionary *tempDic in self.dataArray) {
        if([tempDic[@"isleader"] integerValue] == 1){
            created = YES;
            break;
        }
    }
    if(created == NO && [self.roleType isEqualToString:@"10"]){
        self.createBtn.hidden = NO;
        self.joinBtn.frame = CGRectMake(viewPix(47), buttonTop, viewPix(128), viewPix(46));
        self.joinBtn.layer.borderColor = [UIColor colorWithString:@"#429BFF"].CGColor;
        self.joinBtn.layer.borderWidth = 1;
        self.joinBtn.cornerRidus = 10;
        [self.joinBtn setBackgroundColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.joinBtn setTitleColor:[UIColor colorWithString:@"#429BFF"] forState:UIControlStateNormal];
        
    }else{
        self.createBtn.hidden = YES;
        self.joinBtn.frame = CGRectMake(viewPix(16), buttonTop, Screen_W-viewPix(32), viewPix(48));
        self.joinBtn.layer.borderColor = [UIColor colorWithString:@"#429BFF"].CGColor;
        self.joinBtn.layer.borderWidth = 1;
        self.joinBtn.cornerRidus = 10;
        [self.joinBtn setBackgroundColor:[UIColor colorWithString:@"#429BFF"] forState:UIControlStateNormal];
        [self.joinBtn setTitleColor:[UIColor colorWithString:@"#FFFFFF"] forState:UIControlStateNormal];
    }
    
}


//申请记录
-(void)recordBtnAction{
    LGTeamRecordController *controller = [[LGTeamRecordController alloc]init];
    [self.navigationController pushViewController:controller animated:YES];
}

//加入团队
-(void)joinBtnAction{
    LGJoinTeamController *controller = [[LGJoinTeamController alloc]init];
    [self.navigationController pushViewController:controller animated:YES];
}

//创建团队
-(void)createBtnAction{
    LGCreateTeamController *controller = [[LGCreateTeamController alloc]init];
    controller.backVC = self;
    [self.navigationController pushViewController:controller animated:YES];
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGTeamManageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.dataDic = self.dataArray[indexPath.row];
    cell.lineView.hidden = (indexPath.row == self.dataArray.count-1)?YES:NO;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *tempDic = self.dataArray[indexPath.row];
    NSString *title = [NSString stringWithFormat:@"%@（%@）",tempDic[@"title"],tempDic[@"nums"]];
    LGTeamDetailController *controller = [[LGTeamDetailController alloc]init];
    controller.teamId = LGNSString(tempDic[@"teamid"]);
    controller.title = title;
    [self.navigationController pushViewController:controller animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.001;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, 0.001)];
}

#pragma mark
#pragma mark ====> 创建控件
-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight-viewPix(64)-bottomSafeBarHeight/2.0) style:UITableViewStylePlain];
        [_tableView registerClass:[LGTeamManageCell class] forCellReuseIdentifier:@"cell"];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.rowHeight = viewPix(66);
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = [[LGCornerHeaderView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(25))];
        _tableView.tableFooterView = [[LGCornerFooterView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(25))];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestData)];
    }
    return _tableView;
}


-(UIButton *)recordBtn{
    if(!_recordBtn){
        _recordBtn = [UIButton buttonWithTitle:@"申请记录" titleFont:LGFontWeight(14, UIFontWeightMedium) textColor:@"#429BFF" imageName:@""];
        _recordBtn.frame = CGRectMake(0, 0, viewPix(60), 44);
        [_recordBtn addTarget:self action:@selector(recordBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _recordBtn;
}

-(UIButton *)joinBtn{
    if(!_joinBtn){
        _joinBtn = [UIButton buttonWithTitle:@"加入团队" topMargin:buttonTop];
        _joinBtn.titleLabel.font = LGFontWeight(16, UIFontWeightMedium);
        [_joinBtn addTarget:self action:@selector(joinBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _joinBtn;
}

-(UIButton *)createBtn{
    if(!_createBtn){
        _createBtn = [UIButton buttonWithTitle:@"创建团队" titleFont:LGFontWeight(16, UIFontWeightMedium) textColor:@"#FFFFFF" imageName:@""];
        _createBtn.frame = CGRectMake(viewPix(200), buttonTop, viewPix(128), viewPix(46));
        _createBtn.backgroundColor = [UIColor colorWithString:@"#429BFF"];
        _createBtn.hidden = YES;
        _createBtn.cornerRidus = 10;
        [_createBtn addTarget:self action:@selector(createBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _createBtn;
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
